package models;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import play.db.jpa.Model;

/**
 * @author AkrAmAtion
 * */
@Entity
public class DuplicateMark extends Model {

	@ManyToOne
	public User marker;
	@ManyToOne
	public Idea marked;
	@ManyToOne
	public Idea original;
	public boolean reviewed;
	public boolean accepted;

	/**
	 * @param marker
	 *            is the {@link User} who made this mark
	 * 
	 * @param marked
	 *            is the {@link Idea} that was marked as duplicate
	 * 
	 * @param original
	 *            is the {@link Idea} that is thought was originally published
	 *            and is similar to the marked one.
	 * */
	public DuplicateMark(User marker, Idea marked, Idea original) {
		this.marker = marker;
		this.marked = marked;
		this.original = original;
		marker.addDuplicateMark(this);
		marked.addDuplicateMarkAsMarked(this);
		original.addDuplicateMarkAsOriginal(this);
		reviewed = false;
		accepted = false;
		this.save();
	}

	/**
	 * @author Ahmed Mahmoud
	 * @return
	 * 
	 */
	public boolean cascadeDelete() {
		marker.duplicateMarks.remove(this);
		marker.save();
		marked.duplicateMarksAsMarked.remove(this);
		marked.save();
		original.duplicateMarksAsOriginal.remove(this);
		original.save();
		this.delete();
		return true;
	}

}
